package com.gopay.bis.ccb.bcdl.operation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.ccb.bcdl.BaseReqBody;
import com.gopay.common.exception.GopayException;

public class CcbBcdlManager {
	private final static Logger logger = LoggerFactory.getLogger("BANK_LOG");
	
	private CcbBcdlCorpHelper corpHelper;
	
	private CcbBcdlPersonHelper personHelper;
	
	private CcbBcdlQueryHelper queryHelper;
	
	private CcbBcdlQueryBalanceHelper queryBalanceHelper;

	private CcbBcdlCorpDccQueryHelper dccQueryHelper;

	private SocketTransport bcdlSocketTransport;
	
	private String userId;
	
	private String hosts;
	
	public Object query(Object object) throws GopayException {
		BaseReqBody req = (BaseReqBody) object;
//		String another = "";
//        String hosts = "";
//        
//        for (String str : urls) {
//            if (channles.equals(str)) {
//                hosts = str;
//            } else {
//                another = str;
//            }
//        }
		try {
			req.setUrl(hosts);
			req.setUserId(userId);
			logger.info("发送前置机地址："+hosts);
			return queryHelper.query(req,bcdlSocketTransport);
		}catch(GopayException e){
			if(ProcCodeConstants.PROC_CODE_100E6047.equals(e.getErrCode())){
//                channles=another;
                throw  new GopayException(ProcCodeConstants.PROC_CODE_100E6047,"前置机地址: "+ hosts);
            }else{
                throw new GopayException(ProcCodeConstants.PROC_CODE_100E6049);
            }
		}catch(Exception ex){
			logger.info("ccb ApQueryReq error: ", ex);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6049);
		}
	}
	
	public Object eft(Object object) throws GopayException{
		BaseReqBody req = (BaseReqBody) object;
//		String another = "";
//        String hosts = "";
//        
//        for (String str : urls) {
//            if (channles.startsWith(str)) {
//                hosts = str;
//            } else {
//                another = str;
//            }
//        }
		try{
			req.setUrl(hosts);
			req.setUserId(userId);
			logger.info("发送前置机地址："+hosts);
			if(BankConstants.DCBL_CORP==req.getDcblType()){
				return corpHelper.pay(req,bcdlSocketTransport);
			}else if(BankConstants.DCBL_PERSON==req.getDcblType()){
				return personHelper.pay(req,bcdlSocketTransport);
			}else{
				logger.info("ccb ApPersonReq error: 不支持的交易类型");
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E6045);
			}
		} catch(GopayException e){
		    if(ProcCodeConstants.PROC_CODE_100E6047.equals(e.getErrCode())){
//                channles=another;
                throw  new GopayException(ProcCodeConstants.PROC_CODE_100E6047,"前置机地址: "+ hosts);
            }else{
                throw new GopayException(ProcCodeConstants.PROC_CODE_100E6045);
            }
		} catch (Exception ex) {
			logger.info("ccb ApPersonReq error: ", ex);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6045);
		}
	}
	
	public Object queryBalance(Object object) throws GopayException {
		BaseReqBody req = (BaseReqBody) object;
//		String another = "";
//        String hosts = "";
//        
//        for (String str : urls) {
//            if (channles.startsWith(str)) {
//                hosts = str;
//            } else {
//                another = str;
//            }
//        }
        try{
	        req.setUrl(hosts);
			req.setUserId(userId);
			logger.info("发送前置机地址："+hosts);
			return queryBalanceHelper.queryBalance(req,bcdlSocketTransport);
        } catch(GopayException e){
		    if(ProcCodeConstants.PROC_CODE_100E6047.equals(e.getErrCode())){
//                channles=another;
                throw  new GopayException(ProcCodeConstants.PROC_CODE_100E6047,"前置机地址: "+ hosts);
            }else{
                throw new GopayException(ProcCodeConstants.PROC_CODE_100E6045);
            }
		} catch (Exception ex) {
			logger.info("ccb ApPersonReq error: ", ex);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6045);
		}
	}


	/**
	 * （6WY101）一点接入活期账户明细查询
	 * @param object
	 * ssj
	 * @return
	 * @throws GopayException
	 */
	public Object queryAcctDetail(Object object) throws GopayException {
		BaseReqBody req = (BaseReqBody) object;
		try {
			req.setUrl(hosts);
			req.setUserId(userId);
			logger.info("发送前置机地址："+hosts);
			return dccQueryHelper.query(req,bcdlSocketTransport);
		}catch(GopayException e){
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E6049,"银企直连类交易查询异常");
		}catch(Exception ex){
			logger.info("ccb queryAcctDetail error: ", ex);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6049,"银企直连类交易查询异常");
		}
	}

	public void setCorpHelper(CcbBcdlCorpHelper corpHelper) {
		this.corpHelper = corpHelper;
	}
	
	public void setPersonHelper(CcbBcdlPersonHelper personHelper) {
		this.personHelper = personHelper;
	}

	public void setQueryHelper(CcbBcdlQueryHelper queryHelper) {
		this.queryHelper = queryHelper;
	}

	public void setQueryBalanceHelper(
			CcbBcdlQueryBalanceHelper queryBalanceHelper) {
		this.queryBalanceHelper = queryBalanceHelper;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public void setHosts(String hosts) {
		this.hosts = hosts;
	}

	public void setBcdlSocketTransport(SocketTransport bcdlSocketTransport) {
		this.bcdlSocketTransport = bcdlSocketTransport;
	}

	public void setDccQueryHelper(CcbBcdlCorpDccQueryHelper dccQueryHelper) {
		this.dccQueryHelper = dccQueryHelper;
	}
}
